package util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class MessageUtil {
	  public static final String MESSAGE_TEXT = "text";
	    public static final String MESSAGE_IMAGE = "image";
	    public static final String MESSAGE_VOICE = "voice";
	    public static final String MESSAGE_VIDEO = "video";
	    public static final String MESSAGE_LINK = "link";
	    public static final String MESSAGE_LOCATION = "location";
	    public static final String MESSAGE_EVENT = "event";

	    public static final String EVENT_SUB = "subscribe";
	    public static final String EVENT_UNSUB = "unsubscribe";
	    public static final String EVENT_CLICK = "CLICK";
	    public static final String EVENT_VIEW = "VIEW";

	    /**
	     * xml转为map
	     * @param request
	     * @return
	     * @throws DocumentException
	     * @throws IOException
	     */
	    public static Map<String, String> xmlToMap(HttpServletRequest request ) throws Exception, IOException
	    {
	        Map<String,String> map = new HashMap<String, String>();

	        SAXReader reader = new SAXReader();

	        InputStream ins = request.getInputStream();
	        Document doc = reader.read(ins);

	        Element root = doc.getRootElement();
	        List<Element> list = root.elements();
	        for (Element e : list) {
	            map.put(e.getName(), e.getText());
	        }
	        ins.close();
	        return map;
	    }

}
